package cn.sugarscat.order;

import cn.sugarscat.db.DB;
import cn.sugarscat.flight.SeatManager;

import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderManager {
    public static boolean insertOrder(Order order) {
        DB db = new DB();
        boolean ok = false;
        String sql = "INSERT INTO `order`(" +
                "`user_id`, " +
                "`flight_id`, " +
                "`order_passenger_name`, " +
                "`order_passenger_gender`, " +
                "`order_passenger_dob`, " +
                "`seat_id`, " +
                "`order_state`) VALUES ('" +
                order.getUserId() + "', '" +
                order.getFlightId() + "', '" +
                order.getPassengerName() + "', '" +
                order.getPassengerGender() + "', '" +
                order.getPassengerDob() + "', '" +
                order.getSeatId() + "', '" +
                order.getStatus() +  "');";
        int num = db.executeInsert(sql);
        if(num>0)
            ok = true;
        db.closeDB();
        return ok;
    }

    public static int queryOrderInfo(int uid, int fightId) {
        DB db = new DB();
        int orderId = -1;
        String sql = "SELECT * FROM `order` WHERE `user_id` = ? AND `flight_id` = ? AND `order_state` IN ('已支付', '未支付')";
        ResultSet res = db.executeQuery(sql, ""+uid, ""+fightId);
        try {
            if (res.next()) {
                orderId = res.getInt("order_id");
            }
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return orderId;
    }

    public static Order queryOrderInfo(int orderId) {
        DB db = new DB();
        Order order = new Order();
        String sql = "SELECT * FROM `order` WHERE `order_id` = ?";
        ResultSet res = db.executeQuery(sql,  ""+orderId);
        try {
            if (res.next()) {
                order.setId(res.getInt("order_id"));
                order.setUserId(res.getInt("user_id"));
                order.setFlightId(res.getInt("flight_id"));
                order.setPassengerName(res.getString("order_passenger_name"));
                order.setPassengerGender(res.getString("order_passenger_gender"));
                order.setPassengerDob(res.getString("order_passenger_dob"));
                order.setSeatId(res.getInt("seat_id"));
                order.setStatus(res.getString("order_state"));
            }
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return order;
    }

    public static void updateOrderStatus(int orderId, String state) {
        DB db = new DB();
        String sql = "UPDATE `order` SET `order_state` = ? WHERE `order_id` = ?;";
        db.executeUpdate(sql, state, ""+orderId);
        db.closeDB();
    }

    public static void queryOrders(int uid) {
        DB db = new DB();
        String sql = "SELECT * FROM `order_info_view` WHERE `user_id` = ? ";
        ResultSet res = db.executeQuery(sql, ""+uid);
        printOrders(db, res);
    }

    public static void cancelOrder(int orderId) {
        updateOrderStatus(orderId, "已取消");
        PaymentManager.deletePayment(orderId);
        Order order = queryOrderInfo(orderId);
        SeatManager.cancelBookSeat(order.getSeatId());
        System.out.println("取消成功！");
    }

    public static void queryCancelOrders() {
        DB db = new DB();
        String sql = "SELECT * FROM `order_info_view` WHERE `order_state` = ? ";
        ResultSet res = db.executeQuery(sql, "已取消");
        printOrders(db, res);
    }

    private static void printOrders(DB db, ResultSet res) {
        try {
            System.out.println("订单ID\t航班号\t乘客姓名\t乘客性别\t乘客出生日期\t座位号\t订单状态");
            while (res.next()) {
                System.out.println(res.getInt("order_id") + "\t" +
                        res.getString("flight_number") + "\t\t" +
                        res.getString("order_passenger_name") + "\t\t" +
                        res.getString("order_passenger_gender") + "\t\t" +
                        res.getString("order_passenger_dob") + "\t\t" +
                        res.getString("seat_number") + "\t\t" +
                        res.getString("order_state"));
            }
            res.close();
            db.closeDB();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
